/**
 * 步数控制器
 */

import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../base/EnumIndex";
import { EventManager } from "../manager/EventManager";
import { UIManager } from "../manager/UIManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class StepItem extends cc.Component {
    @property(cc.Label)
    stepLab: cc.Label = null;

    step: number = 0;//当前步数
    onLoad() {
        EventManager.ins.on(EVENT_NAME_ENUM.ADD_STEP, this.addStep, this);
    }
    onDestroy() {
        EventManager.ins.off(EVENT_NAME_ENUM.ADD_STEP, this.addStep, this);
    }
    initStep(step: number) {
        this.step = step;
        this.updateStepLabel();
    }

    /** 刷新步数UI */
    updateStepLabel() {
        this.stepLab.string = this.step.toString();
    }

    /** 加步数 */
    addStep(num = 2, noStepCallback?: Function) {
        this.step += num;
        if (this.step <= 0) {
            this.step = 0;
            //游戏结束
            if (noStepCallback) {
                noStepCallback();
            } else {
                UIManager.ins.showPop('StepUI', BUNDLE_TYPE_ENUM.GAME);
            }
        }
        this.updateStepLabel();
    }
}